package com.a16.yuejian.yuejian.activity;

import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.VideoView;

import com.a16.yuejian.yuejian.R;

import java.util.Formatter;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;


public class ClubVideoActivity extends AppCompatActivity implements View.OnClickListener,MediaPlayer.OnCompletionListener,MediaPlayer.OnPreparedListener,SeekBar.OnSeekBarChangeListener {
    private VideoView club_videoView;
    private SeekBar club_seekBar;
    private String video ="http://10.0.2.2:8080/neu_yuejian201/public/photo/123456.mp4";
    private Timer timer;
    private TimerTask task;
    private ImageView club_video_back,club_video_stop;
    private TextView time3,time4;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_club_video);
        initView();
        play();
        initEvent();
    }

    private void play() {
        if (!TextUtils.isEmpty(video)){

            Uri uri = Uri.parse(video);
            MediaController mc = new MediaController(this);
            mc.setVisibility(View.INVISIBLE);
            club_videoView.setMediaController(mc);
            club_videoView.setVideoURI(uri);
            club_videoView.requestFocus();
            club_videoView.start();
        }

    }

    private void initEvent() {
        club_videoView.setOnPreparedListener(this);
        club_videoView.setOnCompletionListener(this);
        club_seekBar.setOnSeekBarChangeListener(this);
        club_video_back.setOnClickListener(this);
        club_video_stop.setOnClickListener(this);

    }

    private void initView() {
        club_videoView = (VideoView)findViewById(R.id.club_videoView);
        club_seekBar = (SeekBar)findViewById(R.id.club_seekBar);
        club_video_back = (ImageView)findViewById(R.id.club_video_back);
        club_video_stop = (ImageView)findViewById(R.id.club_video_stop);
        time3 = (TextView)findViewById(R.id.videotime3);
        time4 = (TextView)findViewById(R.id.videotime4);

    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        club_videoView.seekTo(0);
        club_seekBar.setProgress(0);
        club_video_stop.setImageResource(R.drawable.ic_play);

    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        int time = club_videoView.getDuration();
        time4.setText(getTime(time));

        club_seekBar.setMax(time);
        mp.start();
        timer = new Timer();
        task = new TimerTask() {
            @Override
            public void run() {
                club_seekBar.setProgress(club_videoView.getCurrentPosition());
            }
        };
        timer.schedule(task, 0, 100);

    }

    private String getTime(int time) {
        StringBuilder mFormatBuilder = new StringBuilder();
        Formatter mFormatter = new Formatter(mFormatBuilder, Locale.getDefault());
        int totalSeconds = time / 1000;
        int seconds = totalSeconds % 60;
        int minutes = (totalSeconds / 60) % 60;
        mFormatBuilder.setLength(0);
        return mFormatter.format("%02d:%02d", minutes, seconds).toString();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.club_video_back:
                finish();
                break;
            case R.id.club_video_stop:
                if (club_videoView.isPlaying()){
                    club_videoView.pause();
                    club_video_stop.setImageResource(R.drawable.ic_play);
                }else {
                    club_videoView.start();
                    club_video_stop.setImageResource(R.drawable.ic_stop);
                }
                break;
        }

    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        time3.setText(getTime(club_videoView.getCurrentPosition()));

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        club_videoView.seekTo(seekBar.getProgress());
        club_videoView.start();

    }
}
